//
//  TechdemoViewController.m
//  Void Dead
//
//  Created by Sidney Just on 16.11.09.
//  Copyright 2009 by Sidney Just.  All rights reserved.
//

#import "TechdemoViewController.h"

@implementation TechdemoViewController

#pragma mark -
#pragma mark Events


- (void)vdEventFired:(vdEventReceiver *)sender {
	if(sender.lastEvent->type == vdInputEvent)
	{
		vdInputMessage *message = (vdInputMessage *)sender.lastEvent->data;
		
		if(message->event == vdTouchesMoved)
		{
			worldMap.pos_x += message->diff[0].x;
			worldMap.pos_y += message->diff[0].y;
		}
	} 
	else if(sender.lastEvent->type == vdErrorEvent) 
	{
		vdErrorMessage *message = (vdErrorMessage *)sender.lastEvent->data;
		
		if(message->event == vdOpenGLError)
		{
			if(message->glError == GL_INVALID_ENUM)
				vdLog(@"OpenGL Error: Invalid Enum in tick %d", [vdKernel sharedKernel].tick);
			
			if(message->glError == GL_INVALID_VALUE)
				vdLog(@"OpenGL Error: Invalid value in tick %d", [vdKernel sharedKernel].tick);
			
			if(message->glError == GL_INVALID_OPERATION)
				vdLog(@"OpenGL Error: Invalid operation in tick %d", [vdKernel sharedKernel].tick);
			
			if(message->glError == GL_STACK_OVERFLOW)
				vdLog(@"OpenGL Error: Stack overflow in tick %d", [vdKernel sharedKernel].tick);
			
			if(message->glError == GL_STACK_UNDERFLOW)
				vdLog(@"OpenGL Error: Stack underflow in tick %d", [vdKernel sharedKernel].tick);
			
			if(message->glError == GL_OUT_OF_MEMORY)
				vdLog(@"OpenGL Error: Out of Memory in tick %d", [vdKernel sharedKernel].tick);

		}
		
		if(message->event == vdEngineError)
		{
			switch (message->vdError) {
				case vdFallback:
					vdLog(@"Engine Error: unsupported OpenGL version.");
					break;
				case vdShaderUnsupported:
					vdLog(@"Engine Error: Shader unsupported. Won't compile shader.");
					break;
				case vdShaderFailedToCompileVertexShader:
					vdLog(@"Engine Error: Failed to compile the vertex shader for %@", sender.lastEvent->sender);
					break;
				case vdShaderFailedToCompileFragmentShader:
					vdLog(@"Engine Error: Failed to compile the fragment shader for %@", sender.lastEvent->sender);
					break;
				case vdShaderFailedToLinkShader:
					vdLog(@"Engine Error: Failed to link shader %@", sender.lastEvent->sender);
					break;
				case vdShaderFailedToLoadShader:
					vdLog(@"Engine Error: Failed to load shader data into %@", sender.lastEvent->sender);
					break;
			}
		}
	}
}

#pragma mark -

- (void)updateLabel:(id)sender {
	static int _ticks = 7;
	
	if(sender == debugLabel)
	{
		_ticks++;
		
		if(_ticks >= 10)
		{
			[debugLabel setString:[NSString stringWithFormat:@"FPS: %i", [vdKernel sharedKernel].fps]];
			_ticks = 0;
		}
	}
}

- (void)viewDidLoad {
	[super viewDidLoad];
	rootNode = [[vdNode alloc] init];
	
	[vdKernel createSharedKernelWithAPI:OpenGLESAPI2];
	[[vdKernel sharedKernel] setMaxFPS:30];
	[[vdKernel sharedKernel] attachToView:self.view];
	[[vdKernel sharedKernel] setRootNode:rootNode];
	[[vdKernel sharedKernel] setOrientation:UIInterfaceOrientationLandscapeLeft];
	[[vdKernel sharedKernel] startRendering];
	
	receiver = [vdEventReceiver receiverWithDelegate:self andType:vdEngineEvent | vdInputEvent | vdErrorEvent];
	[[vdEventHandler sharedEventHandler] addReceiver:receiver];
	
	worldMap = [[vdTMXNode alloc] initWithMap:@"DemoMap.tmx"]; // load the tmx file
	[rootNode addChild:worldMap]; 
	
	debugLabel = [vdLabel labelWithText:@"FPS: 0"];
	debugLabel.pos_x = 5;
	debugLabel.pos_y = 5;
	
	[debugLabel addAction:vdCreateAction(self, @selector(updateLabel:))];
	[rootNode addChild:debugLabel];
}

- (void)didReceiveMemoryWarning {
	[[vdTexManager sharedManager] removeUnusedTextures]; // We can remove all unused textures to free some memory
	NSLog(@"didReceiveMemoryWarning...");
	
    [super didReceiveMemoryWarning];
}

- (void)close {
	[[vdKernel sharedKernel] destroy]; // This will not only release the engine itself, it will also release all nodes 
	// attached to the root node, all textures loaded via the vdTexManager and all attached vdEventReceiver
}

- (void)dealloc {
    [super dealloc];
}

@end
